package com.share.service.oa.org.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.share.bean.oa.org.Department;
import com.share.commons.base.query.BaseQuery;
import com.share.commons.base.query.PageModel;
import com.share.dao.oa.org.DepartmentDao;
import com.share.service.oa.org.DepartmentManager;
import com.share.web.formbean.oa.hr.DepartmentForm;

@Service
public class DepartmentManagerImpl implements DepartmentManager {
	
	@Resource 
	private DepartmentDao deptDao ;
	
	public void setDeptDao(DepartmentDao deptDao) {
		this.deptDao = deptDao;
	}

	@Override
	public void deleteDeptManager(String deptNos) throws Exception {
		this.deptDao.deleteDeptDao(deptNos.split(",")) ;
	}

	@Override
	public PageModel<Department> getScrollDataDeptManager(int firstResult,
			int maxResult,DepartmentForm deptVo) throws Exception {
		
		String hql = "select o from Department o" ;
		StringBuffer whereHQL = new StringBuffer() ; ;
		List<Object> param = new ArrayList<Object>() ;
		
		if(null != deptVo) {
			
			if("byOrgCode".equals(deptVo.getType())) {
				whereHQL.append("org_code=?").append(BaseQuery.AND).append("sdeptno is null") ;
				param.add(deptVo.getOrgCode()) ;
			} else if("manager".equals(deptVo.getType())) {
				whereHQL.append("sdeptno is null") ;
			} else if("subDept".equals(deptVo.getType())) {
				whereHQL.append("sdeptno=?") ; 
				param.add(deptVo.getDeptNo()) ;
			} else if(null != deptVo.getStartDate() && null != deptVo.getEndDate()) {
				whereHQL.append("o.deptCreTime>=?").append(BaseQuery.AND).append("o.deptCreTime<=?") ;
				param.add(deptVo.getStartDate()) ;
				param.add(deptVo.getEndDate()) ;
			} else if(null != deptVo.getKeyword() && !"".equals(deptVo.getKeyword())) {
				
				whereHQL.append("o.deptName").append(BaseQuery.LIKE).append("?") ;
				param.add("%"+deptVo.getKeyword()+"%") ;
			}
			
		}
		LinkedHashMap<String,String> orderby = new LinkedHashMap<String,String>() ;
		orderby.put("deptCreTime",BaseQuery.DESC) ;
		
		return this.deptDao.getScrollDeptDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby) ;
	}

	@Override
	public Department loadByIdDeptManager(String deptNo) throws Exception {
		
		return this.deptDao.loadByIdDeptDao(deptNo);
	}

	@Override
	public void saveDeptManager(Department dept) throws Exception {
		this.deptDao.saveDeptDao(dept) ;
	}

	@Override
	public void updateDeptManager(Department dept) throws Exception {
		this.deptDao.updateDeptDao(dept) ;
	}

	@Override
	public List<Department> findDeptManager(DepartmentForm deptVo) throws Exception {
		
		String hql = "select o from Department o" ;
		
		StringBuffer whereHQL = new StringBuffer() ;
		List<Object> param = new ArrayList<Object>() ; 
		if(null != deptVo && !"".equals(deptVo)) {
			if("byOrgCode".equals(deptVo.getType())) {
				whereHQL.append("org_code=?") ;
				param.add(deptVo.getOrgCode()) ;
			}
		}
		LinkedHashMap<String,String> orderby = new LinkedHashMap<String,String>() ;
		orderby.put("deptCreTime",BaseQuery.DESC) ;
		
		List<Department> list = this.deptDao.findDeptDao(hql, whereHQL.toString(), param, orderby) ;
		
		return list ;
	}
	
	
	/** 排序算法，暂时没空写 */
	public void aaa(List<Department> list) {
		System.out.println("个数："+list.size());
		for (Iterator<Department> iterator = list.iterator(); iterator.hasNext();) {
			Department d = (Department) iterator.next();
			System.out.println("ID："+d.getDeptNo() + "  DeptName："+d.getDeptName() + "  PID："+d.getDeptParent());
		}
		
	}

}
